package org.csie.jimms.java.example;

import java.lang.reflect.Field;

import org.json.JSONException;
import org.json.JSONObject;

public abstract class MyJSONObject implements JSONable {
	@Override
	public String toJSONString() throws IllegalArgumentException, IllegalAccessException, JSONException {
		JSONObject json = new JSONObject();
		Class<MyJSONObject> myClass = (Class<MyJSONObject>) this.getClass();
		Field[] fieldArray = myClass.getDeclaredFields();
		
		for (Field f: fieldArray) {
			String type = f.getType().getName();
			
			if (type.equals("java.lang.String")) {
				String str = (String) f.get(this);
				
				json.put(f.getName(), str);
			} else if (type.equals("int")) {
				json.put(f.getName(), f.getInt(this));
			}
		}
		return json.toString();
	}
}
